<?php
use Org\Util\Tree;
/**
 * Created by PhpStorm.
 * User: cony
 * Date: 14-2-28
 * Time: 下午3:25
 */
function access($attr, $path, $data, $volume) {
    return strpos(basename($path), '.') === 0       // if file/folder begins with '.' (dot)
        ? !($attr == 'read' || $attr == 'write')    // set read+write to false, other (locked+hidden) set to true
        :  null;                                    // else elFinder decide it itself
}

function set_nav($module,$guide){
    $map=$guide?array('cid'=>$guide):'';
    switch($module){
        case 'news':
            return U('/news/index',$map);
            break;
        case 'product':
            return(U('/product/index',$map));
            break;
        case 'message':
            return(U('/message/index'));
            break;
        case 'link':
            break;
        case 'page':
            $m_page=M('page');
            $ename=$m_page->where('id='.$guide)->getField('unique_id');
            return(U('/page/index',array('name'=>$ename)));
            break;
        default:
            return(U('/index/index'));
            break;
    }

}

/**
 * 获取分类-类型
 * @param unknown $id
 * date:2016年12月19日
 * author: EK_熊
 */
function get_cate_name($id){
    return M('category')->where(['cid'=>$id])->getfield('name');
}

/**
 * 
 * @param string $pid       只获取关于父级id的数据
 * @param string $selectcid 选中的cid
 * @return string
 * date:2016年12月19日
 * author: EK_熊
 */
function get_cate_select($pid=false,$selectcid=''){
    $result = F('category');
    if (!$result){
        $result = M('Category')->order("listorder")->select();
        F('category',$result);
    }
    $tree = new Tree();
    $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
    $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
    
    if (strpos($selectcid,',')){
        $selectcid = explode(',',$selectcid);
    }
    foreach ($result as $r) {
        $r['style'] = $r['pid'] == 0 ? "style='color:red;'" : '';
        $r['str_manage'] = '<a href="' . U("add", array("pid" => $r['cid'])) . '">添加子分类</a> | <a href="' . U("edit", array("id" => $r['cid'])) . '">编辑</a>';
        $r['id']=$r['cid'];
        $r['parentid']=$r['pid'];
        if (is_array($selectcid)){
            for($i=0;$i<count($selectcid);$i++){
                if ($selectcid[$i] == $r['cid']) {
                    $r['selected'] = "selected";
                }
            }
        }else{
            
            $r['selected'] = $selectcid == $r['cid'] ? "selected" :'';
        }
        $r['catname'] = $r['name'];
        $array[] = $r;
    }
    
    $tree->init($array);
    $str = "<option value='\$id' catename='\$catname' \$selected>\$spacer\$name</option>";
    $option = $tree->get_tree($pid, $str);
    
    return $option;

}





